package com.deyuanyun.pic.eqpt.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.MapUtil;
import com.deyuanyun.pic.common.util.RequestUtil;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.eqpt.domain.ExtemalUse;
import com.deyuanyun.pic.eqpt.service.ExtemalUseService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Bill.li on 2015/12/30.外部借用
 */
@Controller
@RequestMapping("/eqpt/equipment/extemalUse")
public class ExtemalUseController  extends EqptBaseController{
    @Resource
    private ExtemalUseService extemalUseService;
    
    @RequestMapping(value="/extemalUsePage.html",method= RequestMethod.GET)
    public ModelAndView extemalUse(ModelAndView mav)
    {//主页
        mav.setViewName("/eqpt/extemalUseManagement");
        return mav;
    }
    @ResponseBody
    @RequestMapping("/getExtemalUse.json")
    public void getExternally(HttpServletRequest request){
    	HashMap map=RequestUtil.parseRequestParamToHashMap(request);
        PageJsonBean jsonBean=new PageJsonBean();
        List<ExtemalUse> extemalUseList= extemalUseService.getExternally(map);
        jsonBean.setList(extemalUseList);
        AjaxSupport.sendSuccessText("success",jsonBean);
    }
    @ResponseBody
    @RequestMapping(value="/addExtemalUse",method= RequestMethod.POST)
    public void addExternally(ExtemalUse info, HttpServletResponse response){
        
        ModelAndView mav = new ModelAndView();
        try {
        	HashMap param=(HashMap)MapUtil.beanToMap(info);
			boolean status= extemalUseService.addExternally(param);
			if(status)
			{
				mav.addObject("status", "0");
			}else{
				mav.addAllObjects(param);
				mav.addObject("status", "1");
			}
		} catch (Exception e) {
			e.printStackTrace();
			mav.addObject("status", "1");
		}
		AjaxSupport.sendSuccessText("ok",mav.getModel());
    }
    @ResponseBody
    @RequestMapping(value="/updateExtemalUse",method= RequestMethod.POST)
    public void updateExternally(ExtemalUse info, HttpServletResponse response){
        ModelAndView mav = new ModelAndView();
        try {
        	HashMap param=(HashMap)MapUtil.beanToMap(info);
			boolean status= extemalUseService.updateExternally(param);
			if(status)
			{
				mav.addObject("status", "0");
			}else{
				mav.addAllObjects(param);
				mav.addObject("status", "1");
			}
		} catch (Exception e) {
			e.printStackTrace();
			mav.addObject("status", "1");
		}
		AjaxSupport.sendSuccessText("ok",mav.getModel());
    }


    @RequestMapping(value="/delExtemalUse.json",method=RequestMethod.POST)
    @ResponseBody
    public void delExternally(HttpServletRequest request){
       
        HashMap data = RequestUtil.parseRequestParamToHashMap(request);
        ModelAndView mav = new ModelAndView();
        try {
			boolean status=   extemalUseService.delExternally(data);
			if(status)
			{
				mav.addObject("status", "0");
			}else{
				mav.addObject("status", "1");
			}
		} catch (Exception e) {
			e.printStackTrace();
			mav.addObject("status", "1");
		}
		AjaxSupport.sendSuccessText("ok",mav.getModel());

    }
    
    
    @RequestMapping(value="/getTask.json",method=RequestMethod.POST)
    @ResponseBody
    public void getTask(HttpServletRequest request){
        String id=request.getParameter("id");
        List list=extemalUseService.getTask(null);
        
        HashMap result=new HashMap();
        result.put("list", list);
        AjaxSupport.sendSuccessText("success", result);

    }

}
